/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.pangalacteos.util;

import br.com.pangalacteos.dao.HibernateDAO;
import br.com.pangalacteos.modelo.DBField;
import br.com.pangalacteos.modelo.DBFieldType;
import br.com.pangalacteos.modelo.Fornecedor;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Leandro Del Sole
 */
public class Parser {
    public static Date parseDate(String source) throws Exception {
        return new SimpleDateFormat("dd/MM/yyyy").parse(source);
    }
   
    public static Date parseDateNullable(String source) throws Exception {
        if (source == null || source.isEmpty() || source.replace("/", "").trim().isEmpty()) return null;
        return parseDate(source);
    }
    
    public static String formatDate(Date source) throws Exception {
        return new SimpleDateFormat("dd/MM/yyyy").format(source);
    }
    
    public static String formatDateNullable(Date source) throws Exception {
        try{
            return new SimpleDateFormat("dd/MM/yyyy").format(source);
        } catch (Exception e){
            return "";
        }
    }
    
    public static Float parseFloat(String source) {
        return Float.parseFloat(source.replace(",", "."));
    }
    
    public static Float parseFloatNullable(String source) {
        if (source == null || source.isEmpty()) return null;
        return parseFloat(source);
    }
    
    public static Integer parseInt(String source) {
        return Integer.parseInt(source);
    }
    
    public static Integer parseIntNullable(String source) {
        if (source == null || source.isEmpty()) return null;
        try{
            return parseInt(source);
        } catch (Exception e){
            return null;
        }
    }
    
    public static Object descobreTipo(String tipo, DBField campoBD){
        if (tipo == null || tipo.isEmpty()){
            return null;
        }
        
        if (campoBD.getDbFieldType() == DBFieldType.STRING ){
                return tipo;
        }
        if (campoBD.getDbFieldType() == DBFieldType.INTEGER){
            try{
                return Integer.parseInt(tipo);
            } catch (Exception ignored){/*does nothing*/}
        } 
        if (campoBD.getDbFieldType() == DBFieldType.DATE) {
            try{
                return Parser.parseDate(tipo);
            } catch (Exception ignored){/*does nothing*/}
        }
        if (campoBD.getDbFieldType() == DBFieldType.FLOAT){
            try{
                return Float.parseFloat(tipo);
            } catch (Exception ignored){/*does nothing*/}
        }
        if (campoBD.getDbFieldType() == DBFieldType.FORNECEDOR){
            try{
                return new Fornecedor(Integer.parseInt(tipo));
            } catch (Exception ignored){/*does nothing*/}
        }
        return tipo;
    }
    
    public static String paraCampo(Integer source){
        try {
            if (source == null) return "";
            return String.valueOf(source);
        } catch (Exception e){
            return "";
        }
    }
    public static String paraCampo(Date source){
        try {
            if (source == null) return "";
            return formatDate(source);
        } catch (Exception e){
            return "";
        }
    }
    public static String paraCampo(Float source){
        try {
            if (source == null) return "";
            return String.valueOf(source);
        } catch (Exception e){
            return "";
        }
    }
    public static String paraCampo(Fornecedor source){
        try {
            if (source == null) return "";
            return String.valueOf(source.getCdfornecedor());
        } catch (Exception e){
            return "";
        }
    }
    public static String paraCampo(String source){
        try {
            if (source == null) return "";
            return source;
        } catch (Exception e){
            return "";
        }
    }
    public static Fornecedor parseFornecedor(String id) throws Exception {
        Integer intId = Parser.parseIntNullable(id);
        if (intId == null){
            return null;
        }
        if(new HibernateDAO<Fornecedor>().contains(Fornecedor.class, intId) == false){
            return null;
        }
        return new Fornecedor(intId);
    }
}
